const Util = {

	/**
	 * 保存Cookie
	 * @param {String} name 键名
	 * @param {String} value 键值
	 * @returns {Undefined}
	 */
	setCookie: (name, value) => {
		const cookie = `${name}=${value}`;
		document.cookie = cookie;
	},

	/**
	 * 删除Cookie
	 * @param {String} name 键名
	 * @returns {Undefined}
	 */
	deleteCookie: (name) => {
		const exp = new Date();
		exp.setTime(exp.getTime() - 1);
		const cookieValue = Util.getCookie(name);
		if (cookieValue != null) {
			const cookie = `${name}=${cookieValue}; expires= ${exp.toGMTString()}`;
			document.cookie = cookie;
		}
	},

	/**
	 * 读取Cookie
	 * @param {String} name 键名
	 * @returns {String} 键值
	 */
	getCookie: (name) => {
		const cookies = document.cookie.split('; ');
		let cookieValue = null;
		cookies.map((cookie) => {
			const [key, value] = cookie.split('=');
			if (key === name) {
				cookieValue = value;
			}
			return false;
		});
		return cookieValue;
	},

	/**
	 * Storage
	 */
	Storage: {
		set: (storage, data, name, key) => {
			try {
				if (key) {
					const json = storage.getItem(name);
					const original = json == null ? {} : JSON.parse(json);
					original[key] = data;
					storage.setItem(name, JSON.stringify(original));
				} else {
					storage.setItem(name, JSON.stringify(data));
				}
				return true;
			} catch (e) {
				return false;
			}
		},
		get: (storage, name, key) => {
			try {
				const json = storage.getItem(name);
				if (json) {
					const obj = JSON.parse(json);
					if (key) {
						return obj[key] ? obj[key] : null;
					}
					return obj;
				}
				return null;
			} catch (e) {
				return null;
			}
		},
		remove: (storage, name, key) => {
			try {
				if (key) {
					const json = storage.getItem(name);
					const original = JSON.parse(json);
					delete original[key];
					storage.setItem(name, JSON.stringify(original));
				} else {
					storage.removeItem(name);
				}
				return true;
			} catch (e) {
				return false;
			}
		},
		clear: (storage) => {
			try {
				storage.clear();
				return true;
			} catch (e) {
				return false;
			}
		},
	},

	/**
	 * Session Storage
	 */
	Session: {
		set: (data, name, key) => (Util.Storage.set(sessionStorage, data, name, key)),
		get: (name, key) => (Util.Storage.get(sessionStorage, name, key)),
		remove: (name, key) => (Util.Storage.remove(sessionStorage, name, key)),
		clear: () => (Util.Storage.clear(sessionStorage)),
	},

	/**
	 * Local Storage
	 */
	Local: {
		set: (data, name, key) => (Util.Storage.set(localStorage, data, name, key)),
		get: (name, key) => (Util.Storage.get(localStorage, name, key)),
		remove: (name, key) => (Util.Storage.remove(localStorage, name, key)),
		clear: () => (Util.Storage.clear(localStorage)),
	},

	/**
	 * UTC字符串转日期时间数组
	 * @param {String} utc UTC字符串 [ 2018-08-29T10:56:13+0800 ]
	 * @returns {Object} 时间日期数组对象
	 */
	utcDatetimes: (utc) => {
		if (Object.prototype.toString.call(utc) === '[object Date]') {
			return Util.timestampDateTimes(utc);
		}
		const regexp = /([\d]{4})-([\d]{2})-([\d]{2})T([\d]{2}):([\d]{2}):([\d]{2})(?:\+([\d]{2}):([\d]{2}))?/;
		const arr = regexp.exec(utc);
		if (!arr) {
			return null;
		}
		arr.splice(0, 1);
		const [year, month, day, hours, minutes, seconds] = arr;
		return {
			year, month, day, hours, minutes, seconds,
		};
	},

	/**
	 * 时间戳转时间数组
	 */
	timestampDateTimes: (timestamp) => {
		const date = new Date(timestamp);
		return {
			year: date.getFullYear(),
			month: date.getMonth() + 1,
			day: date.getDate(),
			hours: date.getHours(),
			minutes: date.getMinutes(),
			seconds: date.getSeconds(),
		};
	},

	/**
	 * 时间日期数组格式化
	 * @param {Object} value 对象数组
	 * @param {String} format 时间日期格式
	 * @returns {String} 格式化后的时间日期字符串
	 */
	formatDatetimes: (value, format) => {
		const date = {
			'M+': +value.month,
			'd+': +value.day,
			'h+': +value.hours,
			'm+': +value.minutes,
			's+': +value.seconds,
			'q+': Math.floor((+value.month + 3) / 3),
		};
		let dateFormat = format;
		if (/(y+)/.test(dateFormat)) {
			dateFormat = dateFormat.replace(RegExp.$1, (`${value.year}`).substr(4 - RegExp.$1.length));
		}
		for (const key in date) {
			if (new RegExp(`(${key})`).test(dateFormat)) {
				dateFormat = dateFormat.replace(
					RegExp.$1,
					RegExp.$1.length === 1 ? date[key] : `00${date[key]}`.substr(`${date[key]}`.length),
				);
			}
		}
		return dateFormat;
	},

	/**
	 * UTC时间格式化别名
	 */
	formatUTCDate: (utc, format) => (
		Util.formatDatetimes(Util.utcDatetimes(utc), format)
	),

	/**
	 * 时间戳格式化别名
	 */
	formatTimestampDate: (timestamp, format) => (
		Util.formatDatetimes(Util.timestampDateTimes(timestamp), format)
	),

	/**
	 * 秒格式化为天
	 */
	formatSecondsDay: (value, format) => {
		const date = {
			day: 0,
			hours: 0,
			minutes: 0,
			seconds: 0,
		};
		date.seconds = value;
		if (date.seconds > 60) {
			date.minutes = parseInt(date.seconds / 60, 10);
			date.seconds = parseInt(date.seconds % 60, 10);
		}
		if (date.minutes > 60) {
			date.hours = parseInt(date.minutes / 60, 10);
			date.minutes = parseInt(date.minutes % 60, 10);
		}
		if (date.hours > 24) {
			date.day = parseInt(date.hours / 24, 10);
			date.hours = parseInt(date.hours % 24, 10);
		}
		const regexp = {
			'd+': +date.day,
			'h+': +date.hours,
			'm+': +date.minutes,
			's+': +date.seconds,
		};
		let dateFormat = format;
		for (const key in regexp) {
			if (new RegExp(`(${key})`).test(format)) {
				dateFormat = dateFormat.replace(RegExp.$1, regexp[key]);
			}
		}
		return dateFormat;
	},

	/**
	 * 检查数组中是否包含某个元素值
	 * @param {Array} arr 数组
	 * @param {Object} value 元素值
	 * @return {Boolean}
	 */
	inArray: (arr, value) => {
		if (arr !== null && arr !== undefined) {
			if (arr.indexOf && typeof (arr.indexOf) === 'function') {
				const index = arr.indexOf(value);
				if (index >= 0) {
					return true;
				}
			}
		}
		return false;
	},

	/**
	 * 删除数组中某个元素
	 * @param {Array} arr 数组
	 * @param {Object} value 元素值
	 * @return {Boolean}
	 */
	removeArray: (arr, value) => {
		if (arr.indexOf && typeof (arr.indexOf) === 'function') {
			const index = arr.indexOf(value);
			arr.splice(index, 1);
		}
	},

	/**
	 * 检查是否为iOS系统
	 * @return {Boolean}
	 */
	isiOS: () => (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)),

	/**
	 * 检查是否为Android系统
	 * @return {Boolean}
	 */
	isAndroid: () => (navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1),

};

export default Util;
